package com.qupai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qupai.constant.StatusConstant;
import com.qupai.domain.dto.OrderPageQueryDTO;
import com.qupai.domain.entity.OrderDetail;
import com.qupai.domain.entity.Orders;
import com.qupai.domain.result.PageResult;
import com.qupai.domain.vo.OrderDetailVO;
import com.qupai.domain.vo.OrdersVO;
import com.qupai.mapper.OrderDetailMapper;
import com.qupai.mapper.OrdersMapper;
import com.qupai.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
@RequiredArgsConstructor
public class OrderServiceImpl implements IOrderService {

    private final OrdersMapper ordersMapper;
    private final OrderDetailMapper orderDetailMapper;

    @Override
    public PageResult<OrdersVO> page(OrderPageQueryDTO queryDTO) {


        Page<Orders> page = Page.of(queryDTO.getPageNo(), queryDTO.getPageSize());


        if (queryDTO.getSortBy() != null) {
            page.addOrder(new OrderItem().setColumn(queryDTO.getSortBy()).setAsc(queryDTO.getIsAsc()));
        } else {
            page.addOrder(new OrderItem().setColumn("create_time").setAsc(false));
        }

        LambdaQueryWrapper<Orders> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper
                .eq(queryDTO.getStatus() != null, Orders::getStatus, queryDTO.getStatus())
                .gt(queryDTO.getOrderBeginTime() != null, Orders::getCreateTime, queryDTO.getOrderBeginTime())
                .lt(queryDTO.getOrderEndTime() != null, Orders::getCreateTime, queryDTO.getOrderEndTime());

        if(queryDTO.getItemName()!=null) {

            QueryWrapper<OrderDetail> wrapper = new QueryWrapper<OrderDetail>();
            wrapper.lambda()
                    .select(OrderDetail::getOrderId)
                    .like(OrderDetail::getItemName,queryDTO.getItemName());

            List<Long> orderIds = orderDetailMapper.selectList(wrapper)
                    .stream()
                    .map(OrderDetail::getOrderId)
                    .collect(Collectors.toList());

            List<Orders> ordersList = ordersMapper.selectBatchIds(orderIds);
            queryWrapper.in(Orders::getId, orderIds);


        }
        ordersMapper.selectPage(page, queryWrapper);

        PageResult<OrdersVO> pageResult = new PageResult<>();
        pageResult.setTotal(page.getTotal());
        pageResult.setPages(page.getPages());
        List<OrdersVO> ordersVOList = new ArrayList<>();
        BeanUtils.copyProperties(page.getRecords(), ordersVOList);
        pageResult.setLists(ordersVOList);

        return pageResult;
    }

    @Override
    public OrderDetailVO findById(Long id) {

        Orders order = ordersMapper.selectById(id);

        List<OrderDetail> orderDetails = orderDetailMapper.selectList(Wrappers.<OrderDetail>lambdaQuery().eq(OrderDetail::getOrderId, id));

        OrderDetailVO orderDetailVO = new OrderDetailVO();
        orderDetailVO.setOrderDetailList(orderDetails);
        BeanUtils.copyProperties(order, orderDetailVO);
        return orderDetailVO;
    }

    @Override
    public void status(Integer status, Long id, String cannelReason) {



    }
}
